package org.ogv.usada.states;

import org.ogv.OGV;
import org.ogv.usada.models.GP;
import org.ogv.usada.views.HowTo;
import org.ogv.usada.views.Option;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;

public class StartScreen extends ScreenState {
	private TextureRegion 		_bg;
	private Option				_option;
	private HowTo				_howTo;
	private byte				_state;
	private Rectangle			_r_options, _r_howto, _r_play, _r_ency, _r_exit;

	private static final byte STATE_NORMAL 	= 0;
	private static final byte STATE_OPTION 	= 1;
	private static final byte STATE_HOWTO 	= 2;

	public StartScreen(SpriteBatch sb) {
		super(sb);
		_id = GP.$().STATE_START;
		
		_r_options 	= new Rectangle(146, GP.$().GAME_HEIGHT - 676, 88, 159);
		_r_howto	= new Rectangle(343, GP.$().GAME_HEIGHT - 591, 132, 162);
		_r_play		= new Rectangle(598, GP.$().GAME_HEIGHT - 542, 234, 101);
		_r_ency		= new Rectangle(950, GP.$().GAME_HEIGHT - 613, 190, 100);
		_r_exit		= new Rectangle(997, GP.$().GAME_HEIGHT - 745, 219, 47);
	}
	
	@Override
	public void init() {
		_bg = OGV.asset.getTextureRegion(GP.$().RES_TEXR_START_BG);

		_option = new Option(false);
		_howTo	= new HowTo(GP.$().RES_TEXR_HOWTO);
		
		_state = STATE_NORMAL;

		Gdx.gl.glClearColor(GP.$().GAME_BGCOLOR.r, GP.$().GAME_BGCOLOR.g, GP.$().GAME_BGCOLOR.b, GP.$().GAME_BGCOLOR.a);
		
		initializeDoors();
		resume();
	}
	
	@Override
	public void resume() {
		_bgm = OGV.asset.get("data/audio/bgm-main.mp3", Music.class);
		_bgm.play();
		_bgm.setLooping(true);
		_bgm.setVolume(GP.$().MUSIC_VOL);
		startOpeningDoor();
	}
	
	@Override
	public void pause() {
		_bgm.stop();
	}
	
	@Override
	public void destroy() {
		pause();
	}

	@Override
	public void run(float elapsedTime) {		
		////Drawing
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		_sb.begin();
		
		//Normal Draw
		draw();
		
		//Option draw
		if(_state == STATE_OPTION) 
			_option.draw(_sb);
		
		//HowTo draw
		else if(_state == STATE_HOWTO) 
			_howTo.draw(_sb);
		
		//Draw doors
		if(areDoorsActive())
			drawDoors();
		
		////Update
		if(areDoorsActive())
			updateDoors(elapsedTime);
		else if(_state == STATE_NORMAL)
			updateNormal(elapsedTime);
		else if(_state == STATE_OPTION) {
			updateTouchPoint();
			_option.update(elapsedTime, Gdx.input.isTouched(), _tempV3.x, _tempV3.y, _bgm);
			if(_option.lastTouchWasExit())
				_state = STATE_NORMAL;
		} else if(_state == STATE_HOWTO) {
			updateTouchPoint();
			_howTo.update(elapsedTime, Gdx.input.justTouched(), _tempV3.x, _tempV3.y);
			if(_howTo.backTouched())
				_state = STATE_NORMAL;
		}
		
		_sb.end();
	}

	private void draw() {
		//Draw BG
		_sb.draw(_bg, 0, 0);
	}

	private void updateNormal(float elapsedTime) {
		if(Gdx.input.justTouched()) {
			updateTouchPoint();
			
			if(optionClicked(_tempV3)) {
				//Show option
				GP.$().playClickSound();
				_state = STATE_OPTION;
			} else if(howToPlayClicked(_tempV3)) {
				//Go to level select
				GP.$().playClickSound();
				_state = STATE_HOWTO;
			} else if(playClicked(_tempV3)) {
				//Go to next state
				GP.$().playClickSound();
				requestStateChange(GP.$().STATE_LEVEL_SELECT);
			} else if(encyClicked(_tempV3)) {
				
			} else if(exitClicked(_tempV3)) {
				//exit
				GP.$().playClickSound();
				getStateMgr().requestPopState();
			}
		}
	}

	private boolean exitClicked(Vector3 touch) {
		return _r_exit.contains(touch.x, touch.y);
	}

	private boolean encyClicked(Vector3 touch) {
		return _r_ency.contains(touch.x, touch.y);
	}

	private boolean playClicked(Vector3 touch) {
		return _r_play.contains(touch.x, touch.y);
	}

	private boolean howToPlayClicked(Vector3 touch) {
		return _r_howto.contains(touch.x, touch.y);
	}

	private boolean optionClicked(Vector3 touch) {
		return _r_options.contains(touch.x, touch.y);
	}
}
